package com.xbenben.project.wxpush.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbenben.project.wxpush.entity.MotivationalStatement;
import com.xbenben.project.wxpush.mapper.MotivationalStatementDAO;
import com.xbenben.project.wxpush.service.MotivationalStatementService;
import com.xbenben.project.wxpush.service.ProverbService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author yu.zhijia
 * Date:2023/6/16 11:16
 **/
@Slf4j
@Service
public class MotivationalStatementServiceImpl implements MotivationalStatementService {

    @Autowired
    private MotivationalStatementDAO motivationalStatementDAO;
    @Autowired
    private ProverbService proverbService;

    @Override
    public String getMotivationalStatement() {
        return convertMotivationalStatement().toString();
    }

    /**
     * 新增方法
     */
    @Override
    public void saveMotivationalStatement(MotivationalStatement motivationalStatement) {
        motivationalStatementDAO.insert(motivationalStatement);
    }

    @Override
    public MotivationalStatement selectOneRand() {
        return motivationalStatementDAO.selectOneRand();
    }

    public MotivationalStatement convertMotivationalStatement() {
        List<MotivationalStatement> motivationalStatements = motivationalStatementDAO.selectAll();
        for (MotivationalStatement motivationalStatement : motivationalStatements) {
            String motivationalStatement1 = motivationalStatement.getMotivationalStatement();

            int dotIndex = motivationalStatement1.indexOf(".");
            if (dotIndex >= 0 && dotIndex < 4) {
                motivationalStatement1 = motivationalStatement1.substring(dotIndex + 1);
            }
            motivationalStatement.setMotivationalStatement(motivationalStatement1);
            String s = proverbService.translateToEnglish(motivationalStatement1);
            motivationalStatement.setEnglishContent(s);
            motivationalStatementDAO.updateByPrimaryKey(motivationalStatement);
        }

        return null;
    }


    @Override
    public void saveAndGetAphorismList(int ii) throws IOException {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1", "励志名言");
        hashMap.put("2", "人生格言");
        hashMap.put("3", "名言警句");
        hashMap.put("4", "名人名言");
        hashMap.put("5", "读书名言");
        hashMap.put("6", "经典名言");
        hashMap.put("7", "爱情名言");
        hashMap.put("8", "名言佳句");

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "titleID=" + ii);
        Request request = new Request.Builder()
                .url("https://eolink.o.apispace.com/myjj/common/aphorism/getAphorismList")
                .method("POST", body)
                .addHeader("X-APISpace-Token", "3t0apfseijud9t2yik5d3zu4vfj29elh")
                .addHeader("Authorization-Type", "apikey")
                .addHeader("Content-Type", "")
                .build();

        Response response = client.newCall(request).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        //取出全部句子
        JSONArray allProverb = JSONObject.parseArray((String) jsonObject.getJSONArray("result").getJSONObject(0).get("words"));
        List<String> stringList = allProverb.toJavaList(String.class);
        // 遍历解析出的words集合
        for (int i = 0; i < stringList.size(); i++) {
            String word = stringList.get(i);
            int index = word.indexOf("、");
            String result = word.substring(index + 1);
            MotivationalStatement motivationalStatement = new MotivationalStatement();
            //生成 motivationalStatement  set
            motivationalStatement.setMotivationalStatement(result);
            motivationalStatement.setType(ii + "");
            motivationalStatement.setTypeDesc(hashMap.get(ii + ""));
            log.info("更新数据" + JSONObject.toJSONString(motivationalStatement));
            motivationalStatementDAO.insert(motivationalStatement);
            System.out.println(result);
        }
    }


}
